<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Selection extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('scholarship');
	}
	
	public function index() {
		$this->_get_userinfo();
		
		$this->load->helper('ext_form');
		$this->load->helper('table');
			
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'scholarship'));
		$params = array(
			'sponsors' => $this->scholarship->get_sponsor(),
			'role' => $this->userinfo['role']
		);
		
		if ($this->input->get('search') == FALSE) {
			$limit = $this->scholarship->count_all_scholarship(SELEKSI);
			$params['scholarships'] = $this->scholarship->list_offered_scholarship('0', $limit, SELEKSI);
			$params['title'] = 'Daftar Seleksi Beasiswa';
			$params['empty'] = 'Tidak ada beasiswa dalam masa seleksi.';
		} else {
			$name = $this->input->get('name', TRUE);
			$sponsor = $this->input->get('sponsor', TRUE);
			if ($name == FALSE) { $name = ''; }
			if ($sponsor == FALSE) { $sponsor = ''; }
			$limit = $this->scholarship->count_all_scholarship(SELEKSI, $name, $sponsor);
			$params['title'] = 'Daftar Hasil Pencarian Beasiswa';
			$params['empty'] = 'Tidak ada beasiswa dalam masa seleksi sesuai dengan kriteria pencarian.';
			$params['scholarships'] = $this->scholarship->list_offered_scholarship('0', $limit, SELEKSI, $name, $sponsor);
		}
	
		$params['pager'] = get_pager(6);
		$this->load->view('common/scholar_selection_list', $params); 
		$this->_load_common_footer();
	}
	
	public function detail($id = NULL) {
		$this->_get_userinfo();
		if ($id == NULL) {
			redirect($this->userinfo['role'].'/scholar/selection/index', 'location');
			exit;	
		}
		$scholarship = $this->scholarship->get_scholarship_by_id_and_status($id, SELEKSI);
		if ($scholarship == NULL) {
			redirect($this->userinfo['role'].'/scholar/selection/index', 'location');
			exit;
		}
		
		$this->load->helper('table');
		$limit = $this->scholarship->count_reciever_scholarship($scholarship->id, SELEKSI);
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'scholarship'));
		$params = array(
			'role' => $this->userinfo['role'],
			'scholarship' => $scholarship,
			'type' => SELEKSI,
			'status' => 'masa seleksi',
			'title' => 'Daftar Mahasiswa Pendaftar Beasiswa',
			'empty' => 'Tidak ada mahasiswa mendaftar beasiswa ini.',
			'studentstatus' => 'Status Pendaftaran',
			'countertitle' => 'Jumlah Pendaftar',
			'counter' => $this->scholarship->get_selection_counter($id),
			'recievers' => $this->scholarship->get_reciever_scholarship($id, '0', $limit, SELEKSI),
			'pager' => array(
					1 => get_pager(4, 'pager1'),
					2 => get_pager(4, 'pager2'),
					3 => get_pager(4, 'pager3'),
					4 => get_pager(4, 'pager4'),
					5 => get_pager(5, 'pager5')
				)
		);
		$this->load->view('common/scholar_selection_detail', $params);
		$this->_load_common_footer();
	}
	
	public function approve($id = NULL) {
		$this->_get_userinfo();
		if ($id == NULL) {
			redirect($this->userinfo['role'].'/scholar/selection/index', 'location');
			exit;	
		}
		$scholarship = $this->scholarship->get_scholarship_by_id_and_status($id, SELEKSI);
		if ($scholarship == NULL) {
			redirect($this->userinfo['role'].'/scholar/selection/index', 'location');
			exit;
		}
		
		$this->load->helper('ext_form');
		$this->load->helper('table');
		$this->load->library('data');
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'scholarship'));
		$params = array(
			'scholarship' => $scholarship,
			'role' => $this->userinfo['role'],
			'years' => $this->data->list_year(),
			'majors' => $this->data->list_major()
		);
		
		if ($this->input->get('search') == FALSE) {
			$limit = $this->scholarship->count_selection_student($id);
			$params['students'] = $this->scholarship->search_selection_student($id, '0', $limit);
			$params['title'] = 'Daftar Semua Mahasiswa';
			$params['empty'] = 'Tidak ditemukan mahasiswa.';
			$params['gpavalid'] = $this->session->flashdata('gpavalid');
		} else {
			$gpavalid = $this->_validate_gpa($this->input->get('gpa', TRUE));
			if ($gpavalid != '') {
				$this->session->set_flashdata('gpavalid', $gpavalid);
				unset($_GET['search']);
				redirect($this->userinfo['role'].'/scholar/selection/approve/'.$id.'?'.http_build_query($_GET, '', '&'), 'location');
				exit;
			}
			$name = $this->input->get('name', TRUE);
			$year = $this->input->get('year', TRUE);
			$major = $this->input->get('major', TRUE);
			$gpa = $this->input->get('gpa', TRUE);
			if ($name == FALSE) { $name = ''; }
			if ($year == FALSE) { $year = ''; }
			if ($major == FALSE) { $major = ''; }
			if ($gpa == FALSE) { $gpa = 0.00; } else { $gpa = doubleval($gpa); }
			$limit = $this->scholarship->count_selection_student($id, $name, $year, $major, $gpa);
			$params['students'] = $this->scholarship->search_selection_student($id, '0', $limit, $name, $year, $major, $gpa);
			$params['title'] = 'Daftar Hasil Pencarian Mahasiswa';
			$params['empty'] = 'Tidak ditemukan mahasiswa yang sesuai dengan kriteria pencarian.';
		}
		
		$params['pager'] = get_pager(6);
		$this->load->view('common/scholar_selection_approve', $params);
		$this->_load_common_footer();
	}
	
	private function _validate_gpa($str) {
		if ($str == FALSE) {
			return '';
		} else if (preg_match('/^[0-9]{1}(\.[0-9]{1,2}){0,1}$/', $str) == 1) {
			$gpa = doubleval($str);
			if ($gpa < 0 || $gpa > 4) {
				return '<span class="error">Range IPK dari 0.00 - 4.00</span>';
			} else { 
				return '';
			}
		} else {
			return '<span class="error">Format IPK salah. Format yang benar: #.##</span>';
		} 
	}
	
	public function saveverification() {
		if ($this->input->is_ajax_request()) {
			$id = $this->input->post('id');
			if ($id != FALSE) {
				$result = $this->scholarship->reject_registrant($id);
				if ($result == FALSE) {
					$output = array(
						'type' => 'error',
						'status' => 'failed',
						'message' => 'Gagal update penolakan verifikasi beasiswa. Silakan coba beberapa saat lagi.'
					);
					$this->output->set_header('Content-Type: application/json; charset=utf-8');
					echo json_encode($output);
					exit;
				} else {
					$output = array(
						'type' => 'warning',
						'status' => 'success',
						'message' => 'Berhasil update penolakan verifikasi beasiswa.'
					);
					$this->output->set_header('Content-Type: application/json; charset=utf-8');
					echo json_encode($output);
					exit;
				}
			}
			$output = array(
				'type' => 'error',
				'status' => 'failed',
				'message' => 'Gagal update penolakan verifikasi beasiswa. Silakan coba beberapa saat lagi.' 
			);
			$this->output->set_header('Content-Type: application/json; charset=utf-8');
			echo json_encode($output);
			exit;
		} else {
			$this->output->set_status_header('405');
			show_error("The method you requested is not allowed.", 405, "405 Method Not Allowed");
		}
	}
	
	public function saverejection() {
		if ($this->input->is_ajax_request()) {
			$id = $this->input->post('id');
			if ($id != FALSE) {
				$result = $this->scholarship->reject_applicant($id);
				if ($result == FALSE) {
					$output = array(
						'type' => 'error',
						'status' => 'failed',
						'message' => 'Gagal update penolakan beasiswa. Silakan coba beberapa saat lagi.'
					);
					$this->output->set_header('Content-Type: application/json; charset=utf-8');
					echo json_encode($output);
					exit;
				} else {
					$output = array(
						'type' => 'warning',
						'status' => 'success',
						'message' => 'Berhasil update penolakan beasiswa.'
					);
					$this->output->set_header('Content-Type: application/json; charset=utf-8');
					echo json_encode($output);
					exit;
				}
				
			}
			$output = array(
				'type' => 'error',
				'status' => 'failed',
				'message' => 'Gagal update penolakan beasiswa. Silakan coba beberapa saat lagi.' 
			);
			$this->output->set_header('Content-Type: application/json; charset=utf-8');
			echo json_encode($output);
			exit;
		} else {
			$this->output->set_status_header('405');
			show_error("The method you requested is not allowed.", 405, "405 Method Not Allowed");
		}
	}
	
	public function saveapprove() {
		if ($this->input->is_ajax_request()) {
			$checked = $this->input->post('checked');
			$id = $this->input->post('id');
			$name = $this->input->post('name');
			if ($checked != FALSE && $id != FALSE && $name != FALSE) {
				$name = explode('@', $name);
				$result = $this->scholarship->approve_applicant($id, $name[0], $checked);
				if ($result == TRUE && $checked == 'true') {
					$output = array(
						'type' => 'warning',
						'status' => 'success',
						'message' => 'Mahasiswa '.$name[1].' diterima di beasiswa ini.'
					);
					$this->output->set_header('Content-Type: application/json; charset=utf-8');
					echo json_encode($output);
					exit;
				} else if ($result == TRUE && $checked == 'false') {
					$output = array(
						'type' => 'warning',
						'status' => 'success',
						'message' => 'Status mahasiswa '.$name[1].' kembali menjadi terverifikasi.'
					);
					$this->output->set_header('Content-Type: application/json; charset=utf-8');
					echo json_encode($output);
					exit;
				}
			}
			$output = array(
				'type' => 'error',
				'status' => 'failed',
				'message' => 'Gagal update penerimaan beasiswa. Silakan coba beberapa saat lagi.' 
			);
			$this->output->set_header('Content-Type: application/json; charset=utf-8');
			echo json_encode($output);
			exit;
		} else {
			$this->output->set_status_header('405');
			show_error("The method you requested is not allowed.", 405, "405 Method Not Allowed");
		} 
	}
}